#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int N = 10002;

struct Edge {
	int v, nxt, flow;
} e[N * 20];
int cnt = 1, heads[N];
inline void add(int u, int v, int c) {
	e[++cnt].v = v, e[cnt].flow = c;
	e[cnt].nxt = heads[u];
	heads[u] = cnt;
}

int n, m, s, t;
int d[N];
queue< int > q;

bool bfs() {
	memset(d, 0, sizeof(d));
	while (q.size()) q.pop();
	d[s] = 1;
	q.push(s);
	while (q.size()) {
		int x = q.front();
		q.pop();
		for (int i = heads[x]; i; i = e[i].nxt) {
			Edge &edge = e[i];
			if (edge.flow && !d[edge.v]) {
				q.push(edge.v);
				d[edge.v] = d[x] + 1;
				if (edge.v == t) return true;
			}
		}
	}
	return false;
}

int dinic(int x, int flow) {
	if (x == t) return flow;
	int rest = flow, k = 0;
	for (int i = heads[x]; i && rest; i = e[i].nxt) {
		if (e[i].flow && d[e[i].v] == d[x] + 1) {
			k = dinic(e[i].v, min(e[i].flow, rest));
			if (!k) d[e[i].v] = 0;
			e[i].flow -= k;
			e[i ^ 1].flow += k;
			rest -= k;
		}
	}
	return flow - rest;
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
	freopen("testdata.in", "r", stdin);
	freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
	freopen("Dinic.in", "r", stdin);
	freopen("Dinic.out", "w", stdout);
#endif
#endif
	scanf("%d%d%d%d", &n, &m, &s, &t);
	int t1, t2, t3;
	for (int i = 0; i < m; ++i) {
		scanf("%d%d%d", &t1, &t2, &t3);
		add(t1, t2, t3);
		add(t2, t1, 0);
	}
	int flow = 0, ans = 0;
	while (bfs()) {
		while ((flow = dinic(s, 0x3fffffff))) ans += flow;
	}
	printf("%d", ans);
	return 0;
}